package com.ksyun.alert.utils;

import java.sql.Time;
import java.time.LocalTime;

/**
 * 时间转换工具类
 * @author: hantou
 * @date: 2024-05-10
 */
public class TimeParseUtil {

    /**
     * 将时间字符串转换为秒
     * @param timeString 时间字符串
     */
    public static Long parseTimeToSeconds(String timeString) {
        long totalSeconds = 0;
        int currentNumber = 0;
        boolean isParsingNumber = false;

        for (int i = 0; i < timeString.length(); i++) {
            char c = timeString.charAt(i);
            if (Character.isDigit(c)) {
                currentNumber = currentNumber * 10 + Character.getNumericValue(c);
                isParsingNumber = true;
            } else {
                if (isParsingNumber) {
                    if (c == 'h' || c == 'H') {
                        totalSeconds += currentNumber * 3600; // 将小时转换为秒
                    } else if (c == 'm' || c == 'M') {
                        totalSeconds += currentNumber * 60; // 将分钟转换为秒
                    } else if (c == 's' || c == 'S') {
                        totalSeconds += currentNumber; // 秒
                    }
                }
                currentNumber = 0;
                isParsingNumber = false;
            }
        }

        if (isParsingNumber) {
            totalSeconds += currentNumber;
        }

        return totalSeconds;
    }

    /**
     * 获取起始时间戳
     * @param nowTimestamp 当前时间戳
     * @param evaluationPeriod 需要往前回溯的时间段
     * @return 起始时间戳
     */
    private Long getStartTimestamp(Long nowTimestamp, Time evaluationPeriod){
        LocalTime time = evaluationPeriod.toLocalTime();
        int second = time.toSecondOfDay();
        return nowTimestamp - second;
    }
}
